home *** CD-ROM | disk | FTP | other *** search
- // Assignment 6
-
- #include "SAT.h"
-
- short kSpeed = 5;
- SpritePtr ignore;
- Handle theSound;
- long score;
- long startTime;
- Boolean doneFlag;
- short direction;
-
- pascal void SetupTarget (SpritePtr me);
-
- pascal void HandleSprite (SpritePtr me)
- {
- EventRecord event;
- char temp;
-
- // Now hold on to the hat! I'm using GetOSEvent to get key down events.
- // the keydowns then affect the speed variable in the sprite. That speed
- // is added to the position. Finally, I check the position against the screen
- // borders, and modify the speed in case I reach a border.
-
- if (GetOSEvent(keyDownMask, &event))
- {
- if (event.what = keyDown)
- {
- temp = BitAnd(event.message, charCodeMask);
-
- switch (temp)
- {
- case 'a':
- {
- me->speed.h = me->speed.h - 1;
- }
- break;
-
- case 's':
- {
- me->speed.h = me->speed.h + 1;
- }
- break;
-
- case 'w':
- {
- me->speed.v = me->speed.v - 1;
- }
- break;
-
- case 'z':
- {
- me->speed.v = me->speed.v + 1;
- }
- break;
- }
- }
- }
-
- me->position.h = me->position.h + me->speed.h;
- me->position.v = me->position.v + me->speed.v;
- if (me->position.h < 0)
- {
- me->speed.h = !(me->speed.h);
- me->position.h = 0;
- }
- if (me->position.h > gSAT.offSizeH - 32)
- {
- me->speed.h = !(me->speed.h);
- me->position.h = gSAT.offSizeH - 32;
- }
- if (me->position.v < 0)
- {
- me->speed.v = !(me->speed.v);
- me->position.v = 0;
- }
- if (me->position.v > gSAT.offSizeV - 32)
- {
- me->speed.v = !(me->speed.v);
- me->position.v = gSAT.offSizeV - 32;
- }
- }
-
- pascal void SetupSprite (SpritePtr me)
- {
- me->task = &HandleSprite;
- me->face = SATGetFace(128);
- SetRect(&me->hotRect, 0, 0, 32, 32);
- }
-
- pascal void HandleTarget (SpritePtr me)
- {
- // The target sprite isn't modified much. I just limit movement after gSAT.offSizeH instead
- // of a hard-coded constant.
-
- me->position.h = me->position.h + direction;
- if (me->position.h < 0)
- direction = kSpeed;
- if (me->position.h > gSAT.offSizeH - 32)
- direction = -kSpeed;
- }
-
- pascal void HitTarget (SpritePtr me, SpritePtr him)
- {
- SATPort savePort;
- short strLength;
- Str255 str;
- Rect r;
-
- if (him->task = &HandleSprite) // Check what we hit!
- {
- me->task = nil;
- ignore = SATNewSprite(-1, 0, SATRand(gSAT.offSizeV - 32), &SetupTarget);
-
- // We could also re-use the old sprite for a new one, if we like.
-
- SATSoundPlay(theSound, 1, true);
-
- // Add to the score
-
- score = score + 1;
-
- // Draw the score on the screen.
-
- SATGetPort(&savePort); // Save port and device!
- SATSetPortBackScreen();
- SetRect(&r, 100, 0, 100 + 100, 15);
- EraseRect(&r);
- MoveTo(r.left, r.bottom - 3);
- DrawString("\pCaught: ");
- SATDrawLong(score);
- SATBackChanged(&r);
- SATSetPort(&savePort); // Always restore!
-
- if (score == 10)
- doneFlag = true;
- }
- }
-
- pascal void SetupTarget (SpritePtr me)
- {
- me->task = &HandleTarget;
- me->hitTask = &HitTarget;
- me->face = SATGetFace(129);
- SetRect(&me->hotRect, 0, 0, 32, 32);
- direction = kSpeed;
- }
-
- main()
- {
- short kTicksPerFrame = 2;
- long t;
- Str255 str;
-
- SATInitToolbox();
-
- SATConfigure(false, kVPositionSort, kForwardCollision, 32);
- SATInit(128, 129, 478, 302);
- ignore = SATNewSprite(0, 200, 200, &SetupSprite);
- ignore = SATNewSprite(0, 0, SATRand(gSAT.offSizeV), &SetupTarget);
- theSound = SATGetNamedSound("\pTestSound");
- score = 0;
- HideCursor();
- startTime = TickCount();
- doneFlag = false;
- do
- {
- t = TickCount();
- SATRun(true);
- while ((TickCount () - t) < kTicksPerFrame);
- }while (!Button () && !doneFlag);
-
- ShowCursor();
-
- // Extremely simple result report. A real game should, of course, display this in the game
- // window, in a prettier way, perhaps with a high score list, etc.
-
- sprintf(str, "Time to catch %ld disks: %ld seconds.", score, (TickCount() - startTime) / 60);
- c2pstr((char *)str);
- SATReportStr(str);
- SATSoundShutup();
- }